//
//  ViewController.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/4/11.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources


class ViewController: UIViewController {
    var tableView: UITableView!
    let disposeBag = DisposeBag()
    let vcNames = [
        "RxViewController1",
        "RxViewController2",
        "RxViewController3",
        "RxViewController4",
        "RxViewController5",
        "RxViewController6",
    ]


    override func viewDidLoad() {
        super.viewDidLoad()
        //创建表格视图
        self.tableView = UITableView(frame: self.view.frame, style: .plain)
        //创建一个重用的单元格
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)

        //初始化数据
        let items = Observable.just([
            "01 RxSwift基本流程",
            "02 RxSwift基本框架初体验",
            "03 RxSwift在TableView中RX",
            "04 RxDataSource",
            "05 RxSwift封装网络",
            "06 RxSwift绑定ui和viewmodel"
        ])

        //设置单元格数据（其实就是对 cellForRowAt 的封装）
        items.bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
            cell.textLabel?.text = "\(element)"
            return cell
        }.disposed(by: disposeBag)


        tableView.rx.itemSelected.subscribe(onNext: { [weak self] (indexpath) in
            let row = indexpath.row
            guard let vc = getClass(stringName: (self?.vcNames[row])!) else {
                return
            }
            self?.navigationController?.pushViewController(vc, animated: true)

        }).disposed(by: disposeBag)

    }


}


